package com.splic.product.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cmcc.rhsdk.utils.RSAUtils;
import lombok.extern.apachecommons.CommonsLog;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.UUID;

@CommonsLog
public class CheckMobileUtil {

    private static final String checkMobileUrl = "https://onekey2.cmpassport.com/openapi/rs/tokenValidate";

    public static void main(String[] args) throws Exception {
        checkMobile("19155315508"
                , "STsid0000001678629775508dowQ5QA5hWMoWT00gIbwlc4xAuSLlPor"
                , "300002784781"
                , "F42728E548444708A17B989E3960336F"
                , "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsUkJPkVKdGJxWPNT36nC7f+jdoeDoDeWrjN82f0ZUao2ztCP9LttomfEgLyKT6mOIor0wwyEYj9UjREi/87CyV+UEcr5/ug6OnwB0/qaMRr+J+2ttKvhE//ttODM16icrOb+GvpPK5I+BG+bU/zfMIp8KjKTj3hh2bf01KYxXawIDAQAB");
    }

    public static Boolean checkMobile(String phoneNumber, String token, String cmAppId, String cmAppKey, String cusPublicKey) throws Exception {
        String privateKeyStr1 = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJuUfFadQvxcn+giexmK+CaCq4WiQpjtYaiNKHqyrBgTO9KTqyvrUKFZPhZBA8isNyG3tZF9ZyoxOe6YDyZ0kOq4lXfxHzMmAU+Urh06J2wFRC245OTJXY/MqJxtrgdymZLyCX9QwQYx6OrhUvyuZF1BIPJHuwyG9Mo9BDzMt+vrAgMBAAECgYEAgWVG4JbdPp0e1efPsFfXudfWJkfk0eIk5hW5BAuUfFWK3MG6jMWjAv8BV2rtZbpuBBoR5haBdiMJBrrrZtVuLK4uBiDqmgP2hCY2XRkH9U/O06wzsP4E8C9Pu8S6dFgqAu5CVvt7cD4ciR+LfKtxhvAQQGqxSi+uQPkmP9DiBJkCQQDH5yhi260UwTuufggVFhh1BGSVgehXFjNTOwtVN6VraJ76j6yABEh6fcUpBn3iLCFiV9+C+91wxpltiy8vP+43AkEAxz01nJqYWUjxyLWS6OFCZMWMbxhI+sXg4+3y/YCteNphGfKuXZU9KEeq9DYGilzFn/BSJ5ucs6bz0WNXPAE17QJAL/7RFIM32C7Y3HlICYAHmTjXyYmqNhaPdiFMBEbbAaHZ4cDTIt1kZXyC9OmpGb+MuGbutC0yqpwURFfRmV+ZGwJAbagMp8KWcx2opVT6JF5GUnw/zK1LDWp3WjuOb2OxFkDrDIL64NrQrCi1uG87rk9dHagBL3eACYirzOSmEIqCCQJAFIYC33sc7iR46nkRFP5j2kQWHwEm3vsT+gMufbAV2uXxzTWK2u3UkU5ohi6Qc6sXM+2M8STLotFDpSe+9tsbAg==";
        String timestamp = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN);
        String msgId = UUID.randomUUID().toString();
        String version = "1.0";
        JSONObject header = new JSONObject();
        header.putOpt("version", version);
        header.putOpt("msgId", msgId);
        header.putOpt("timestamp", timestamp);
        header.putOpt("appId", cmAppId);
        JSONObject body = new JSONObject();
        body.putOpt("openType", "0");
        body.putOpt("requesterType", "0");
        body.putOpt("keyType", "1");
        body.putOpt("phoneNum", RSAUtils.encryptedDataOnJava((phoneNumber + cmAppKey + timestamp), cusPublicKey.replace("\n", "")));
        body.putOpt("token", token);
        body.putOpt("sign", RSAUtils.sign((cmAppId + msgId + body.getStr("phoneNum") + timestamp + token + version).getBytes(StandardCharsets.UTF_8), privateKeyStr1));
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOpt("header", header);
        jsonObject.putOpt("body", body);
        String res = HttpRequest.post(checkMobileUrl)
                .header(Header.CONTENT_TYPE, "application/json")
                .body(JSONUtil.toJsonStr(jsonObject))
                .timeout(20000)
                .execute().body();
        log.info("本机验证返回" + res);
        JSONObject result = JSONUtil.parseObj(res);
        JSONObject headerRes = result.getJSONObject("header");
        if (headerRes != null) {
            String resultCode = headerRes.getStr("resultCode");
            if (resultCode != null) {
                return "000".equals(resultCode);
            }
        }
        return false;
    }
}
